//
//  MyDownLoadViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-11-9.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "MyDownLoadViewController.h"
#import "DowningLoadTableViewCell.h"
#import "FinishedTableViewCell.h"

@interface MyDownLoadViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UISegmentedControl * segment;
//@property (nonatomic, strong)UITableView * downloadingTable;
//@property (nonatomic, strong)UITableView * finishedTable;

@property(nonatomic) NSMutableDictionary *fileNameDic;

@property(nonatomic,strong) NSMutableArray *tempFileList;

@property (nonatomic, assign)NSInteger tempAllStop;
@end

static MyDownLoadViewController * downSongVC = nil;

@implementation MyDownLoadViewController


+ (MyDownLoadViewController *)shareJKDownVC{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        downSongVC = [[MyDownLoadViewController alloc] init];
    });
    return downSongVC;
}

//播放页面的下载代理方法
- (void)beginDoenload
{
    [[MyDownLoadViewController shareJKDownVC] reloadDataUIData];
}
- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.navigationController.navigationBar.hidden = NO;
    
      self.navigationItem.titleView.userInteractionEnabled = YES;
    

    [self setupFinishUI];
    [self setupDownLoadUI];

}


- (void)setupFinishUI
{

    self.finishedTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
    _finishedTable.delegate = self;
    _finishedTable.dataSource = self;
    [self.view addSubview:_finishedTable];

    
    UITapGestureRecognizer * randomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(randomPlaySongs)];
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    [headerView addGestureRecognizer:randomTap];
    //随机
    UIView * randomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, headerView.height, headerView.height)];
    [headerView addSubview:randomView];
    UIImageView * tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake((randomView.width-20)/2, (randomView.height-20)/2, 20, 20)];
    tempImageView.image = [UIImage imageNamed:@"randomStop@3x.png"];
    [randomView addSubview:tempImageView];
    UILabel * tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(randomView.right, 0, self.view.width-100, headerView.height)];
    tempLabel.text = @"随机播放全部";
    tempLabel.font = FONT_16;
    [headerView addSubview:tempLabel];
    //多选
    UITapGestureRecognizer * moreSelectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreSelectTap)];
    UIView * moreSelectView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width-90, 0, 90, headerView.height)];
    [moreSelectView addGestureRecognizer:moreSelectTap];
    [headerView addSubview:moreSelectView];
    //46X38  moreSelect@3x.png
    UIImageView * moreSelectImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 16)];
    moreSelectImageV.userInteractionEnabled = YES;
    moreSelectImageV.image = [UIImage imageNamed:@"moreSelect@3x.png"];
    [moreSelectView addSubview:moreSelectImageV];
    UILabel * moreSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 60, 30)];
    moreSelectLabel.text = @"批量管理";
    moreSelectLabel.font = FONT_14;
    [moreSelectView addSubview:moreSelectLabel];
    
    
    //下划线
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerView.height-0.5, headerView.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:lineView];
    _finishedTable.tableHeaderView = headerView;

    [self setExtraCellLineHidden:_finishedTable];
    
    //cell分割线顶头
    if ([self.finishedTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.finishedTable setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self.finishedTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.finishedTable setLayoutMargins: UIEdgeInsetsZero];
    }

}

- (void)setupDownLoadUI
{
    
    NSLog(@"%f", self.view.height);
    
    self.downSongTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height) style:UITableViewStylePlain];
    _downSongTable.delegate = self;
    _downSongTable.dataSource = self;
    _downSongTable.hidden = YES;
    [self.view addSubview:_downSongTable];
    
    NSLog(@"%f", _downSongTable.height);
    
    
    UITapGestureRecognizer * randomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(randomPlaySongs)];
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    [headerView addGestureRecognizer:randomTap];
    //随机
    UIView * randomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, headerView.height, headerView.height)];
    [headerView addSubview:randomView];
    UIImageView * tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake((randomView.width-20)/2, (randomView.height-20)/2, 20, 20)];
    tempImageView.image = [UIImage imageNamed:@"randomStop@3x.png"];
    [randomView addSubview:tempImageView];
    UILabel * tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(randomView.right, 0, self.view.width-100, headerView.height)];
    tempLabel.text = @"暂停当前下载";
    tempLabel.font = FONT_16;
    [headerView addSubview:tempLabel];
    //多选
    UITapGestureRecognizer * moreSelectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreSelectTap)];
    UIView * moreSelectView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width-90, 0, 90, headerView.height)];
    [moreSelectView addGestureRecognizer:moreSelectTap];
    [headerView addSubview:moreSelectView];
    //46X38  moreSelect@3x.png
    UIImageView * moreSelectImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 16)];
    moreSelectImageV.userInteractionEnabled = YES;
    moreSelectImageV.image = [UIImage imageNamed:@"moreSelect@3x.png"];
    [moreSelectView addSubview:moreSelectImageV];
    UILabel * moreSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 60, 30)];
    moreSelectLabel.text = @"批量管理";
    moreSelectLabel.font = FONT_14;
    [moreSelectView addSubview:moreSelectLabel];
    
    
    //下划线
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerView.height-0.5, headerView.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:lineView];
    _downSongTable.tableHeaderView = headerView;
    
    
    [self setExtraCellLineHidden:_downSongTable];
    
    
    //cell分割线顶头
    if ([self.downSongTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.downSongTable setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self.downSongTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.downSongTable setLayoutMargins: UIEdgeInsetsZero];
    }
    
    
    
    
}

- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}


- (void)leftBtnAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)changeUI:(UISegmentedControl *)segment
{
    if (_segment.selectedSegmentIndex == 0) {
        _downSongTable.hidden = YES;
        _finishedTable.hidden = NO;
        [self.finishedTable reloadData];
    }
    if (_segment.selectedSegmentIndex == 1) {
        _downSongTable.hidden = NO;
        _finishedTable.hidden = YES;
        [self.downSongTable reloadData];
    }

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}


@end
